package com.edu.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.edu.filter.LogingFilter;
import com.edu.servlet.MyServlet;
import com.edu.servlet.WebServletFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;

/**
 * 所有关于页面的整合都在这里
 *
 * @作者 five-five
 * @创建时间 2021/1/11
 */
@PropertySource(value = {"classpath:/ftp/ftpconn.properties", "classpath:/front.properties"})
//@ServletComponentScan(value = {"com.edu.servlet"})
@Configuration
//@EnableWebMvc//全面接管SpringBoot(写了这个注解，就相当于ssm开发了)
public class WebAppConfig implements WebMvcConfigurer {
    /**
     * 视图解析器，不想写后缀.html
     *
     * @return
     */
//    @Bean
//    @Primary
//    @ConditionalOnMissingBean
    public InternalResourceViewResolver defaultViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/");
        resolver.setSuffix("html");
        return resolver;
    }

    /**
     * 添加拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LogingFilter())
                .addPathPatterns("/login", "")
                .excludePathPatterns("/list", "/static", "/templates", "/img");
    }

    /**
     * 用于注册原生的servlet、listener以及filter
     *
     * @return
     */
//    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(new MyServlet());
        servletRegistrationBean.setUrlMappings(Arrays.asList(new String[]{"/servlet","/*"}));
        return servletRegistrationBean;
    }

    /**
     * 用于注册原生的servlet、listener以及filter
     * @return
     */
//    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
//        return new FilterRegistrationBean(new WebServletFilter(), servletRegistrationBean());//继承路径写法
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebServletFilter());
        filterRegistrationBean.setUrlPatterns(Arrays.asList(new String[]{"/*","/"}));
        return filterRegistrationBean;
    }

    /**
     * 整合Swagger文档
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)//配置了Swagger的Docket的bean实例
                .pathMapping("/")
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.edu.controller"))//配置扫描那个包
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("欢迎来到Swagger调试界面")
                        .description("SpringBoot整合Swagger，详细信息......")
                        .version("1.0")
                        .contact(new Contact("fivefive", "http://www.cnblogs.com/five-five", "1571379055@qq.com"))
                        .license("The Apache License")
                        .licenseUrl("http://www.cnblogs.com/five-five")
                        .termsOfServiceUrl("fivefive的散人协会")
                        .extensions(new ArrayList<>())
                        .build());
    }

}
